//
// Created by nilyang on 2022/1/25.
// 杨小过2022年初学代码
//
#include <stdio.h>
#include <math.h>
#include "num_split.h"


int main() {
    sum_split();
    sum_split_teacher();
}
int sum_split() {
    int n;
    int i;
    int sum = 0;
    int summ = 1;
    int max_int = (pow(2,32) -1)/10;// 2^32-1
    do {
        printf("请输入要分解的数字(最大不能超过 %d)! ", max_int);
        //http://c.biancheng.net/view/183.html
        fflush(stdin); //清除输入缓冲，不然会读入上次输入。
        scanf("%d", &n);
        if (n < max_int) {
            break;
        }
    } while (1);
    sum = n;
    // i 计算数字n是几位数
    for (i = 0; n > 0; i++) {
        n /= 10;
        summ *= 10;
        printf("最高分位值是：%d\n", summ);
    }
    summ /= 10;
    n = sum;
    for (; i > 0; i--) {
        // 十分位，百分位，千分位，万分位
        printf("n=%d,是%d位数,分位值summ=%d", n, i, summ);
        sum = n / summ;
        printf(" 分位数是 %d\n", sum);
        n %= summ;
        summ /= 10;
    }
    printf("\n");
    return 0;
}

int sum_split_teacher() {
    int x;
    scanf("%d", &x);

//    x = 10;
    int mask = 1;
    int t = x;
    while (t > 9) {
        t /= 10;
        mask *= 10;
    }

    printf("x=%d, mask=%d\n", x, mask);

    do {
        int d = x / mask;
        printf("d=x(%d)/mask(%d) = %d\n", x, mask, d);
        if (mask > 9) {
            printf(" ");
        }
        x %= mask;
        mask /= 10;
    } while (mask > 0);
    printf("\n");
    return 0;
}
